<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%cms_channel}}".
 *
 * @property int $id
 * @property string $name 频道名称
 * @property string|null $description 频道描述
 * @property string|null $icon 图标
 * @property string|null $bgcolor 背景色
 * @property string|null $link 跳转链接
 * @property int $listorder 排序
 * @property int $status 状态
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class CmsChannel extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_channel}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['listorder', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'description'], 'string', 'max' => 50],
            [['icon', 'link'], 'string', 'max' => 255],
            [['bgcolor'], 'string', 'max' => 7],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '频道名称',
            'description' => '频道描述',
            'icon' => '图标',
            'thumb' => '缩略图',
            'bgcolor' => '背景色',
            'link' => '跳转链接',
            'listorder' => '排序',
            'status' => '状态',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    public static function findActived($condtion = []) {
        return static::find()->active(static::STATUS_ACTIVE)->andFilterWhere($condtion)->orderBy([
            'listorder' => SORT_ASC
        ])->all();
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'name',
            'color' => 'bgcolor',
            'icon' => function() {
                return Url::attach($this->icon);
            },
        ];
    }

}
